$(function() {
    //接口结构
    var data;
    //    var data = {
    //        "returnFlag": "0", //入口
    //        "mobile": {
    //            "number": 13554250365
    //        }
    //    };
    var i = 0; //全局变量序列号

    //初始化
    $.ajax({
        type: "POST",
        url: "LoadWinners",
        dataType: "json",
        success: function(da) {
            var json = da;
            //console.log(data);
            i = json.mobile.length;
            for (var j = 0; j < i; j++) {
                if (json.mobile[j].status == 8) {
                    console.log(json.mobile[j].status);
                    var htmlStr = "";
                    htmlStr += '<li>';
                    htmlStr += '<i class="winner-num">' + (j + 1) + '</i>'
                                    + '<span class="winner-mobile">' + json.mobile[j].phone + '</span>'
                                    + '<span class="local">' + json.mobile[j].remark + '</span>'
                                    + '<span class="zj-record">已确认！</span>';
                    htmlStr += '</li>';
                    $("#winner_list").append(htmlStr);
                }
                else {
                    var htmlStr = "";
                    htmlStr += '<li>';
                    htmlStr += '<i class="winner-num">' + (j + 1) + '</i>'
                                    + '<span class="winner-mobile">' + json.mobile[j].phone + '</span>'
                                    + '<span class="local">' + json.mobile[j].remark + '</span>'
                                    + '<button type="button" class="winner-status winner-valid">确认中奖</button>'
                                    + '<button type="button" class="winner-status winner-invalid">无效号码</button>';
                    htmlStr += '</li>';
                    //console.log(htmlStr);
                    $("#winner_list").append(htmlStr);
                }
            }

        }
    });

    //抽奖弹层
    (function() {
        $("#kj_btn").on("click", function() {
            var len = $("#winner_list").find("li").length;
            if (len == 10) {
                $(this).attr("disabled", "disabled");
            }
            else {
                $.ajax({
                    type: "POST",
                    url: "StartExtractionPhone",
                    dataType: "json",
                    success: function(da) {
                        var res = da;
                        data = res;
                        if(data.mobile.length !== 0){
                        	$("#kj_btn").removeAttr("disabled");
                        	$("#btn").removeAttr("disabled");
                            $("#kj_layer").show();
                            $("body").addClass("layer-bg");
                        }
                        else{
                        	alert("参与人数不足！");
                        	$("#kj_btn").attr("disabled", "disabled");
                        }
                    }
                });
            }
        });
    } ());

    //开始抽奖
    (function() {
        var g_Interval = 1;
        var g_Timer;
        var running = false;
        var btn = $("#btn"); //抽奖按钮
        var result = $("#result"); //抽奖结果
        var mobile_arr = [15697826531, 13203250366, 13067250367, 13886479851, 15986943412, 15636689431, 18971685752, 18637915552, 15786344411, 18546973356]; //设定10个随机变化的号码

        btn.on("click", function() {
            if (running) {
                result.html(data.mobile.phone);
                running = false;
                clearTimeout(g_Timer);
                btn.html("开始").attr("disabled", "disabled");
                setTimeout(function() {
                    $("#kj_layer").hide();
                    $("body").removeClass("layer-bg");
                }, 1000);
            }
            else {
                running = true;
                btn.html("停止");
                beginTimer();
                if (data.returnFlag == 0) {
                    var mobile = data.mobile;

                    //动态添加DOM
                    var htmlStr = "";
                    htmlStr += '<li>';
                    htmlStr += '<i class="winner-num">' + (i + 1) + '</i>'
                                + '<span class="winner-mobile">' + mobile.phone + '</span>'
                                + '<span class="local">' + mobile.remark + '</span>'
                                + '<button type="button" class="winner-status winner-valid">确认中奖</button>'
                                + '<button type="button" class="winner-status winner-invalid">无效号码</button>';
                    htmlStr += '</li>';
                    //console.log(htmlStr);
                    $("#winner_list").append(htmlStr);
                    i++;
                }

                /*$.ajax({
                type: "post",
                url: "",
                dataType: "json",
                success: function(data) {
                var data = data;
                        
                }
                },
                error: function(errorMsg) {
                alert("请求数据失败!");
                }
                });*/
            }

            //随机变号码
            function updateRndNum() {
                for (var i = 0, len = mobile_arr.length; i < len; i++) {
                    var index = Math.floor(Math.random() * 10);
                    var number = mobile_arr[index];
                }
                result.html(number);
            }

            function beginTimer() {
                g_Timer = setTimeout(beat, g_Interval);
            }

            function beat() {
                g_Timer = setTimeout(beat, g_Interval);
                updateRndNum();
            }
        });
    } ());

    //删除无效号码
    (function() {
        $("#winner_list").on("click", function(e) {
            var target = $(e.target);
            var li = target.closest("li");
            if (target.hasClass("winner-invalid")) {
                var index = li.find(".winner-mobile").html();
                i = li.find("i").html();
                li.nextAll().each(function() {
                    $(this).find("i").html(i);
                    i++;
                });
                //console.log(index);
                target.closest("li").remove();

                $.ajax({
                    type: "POST",
                    url: "DeleteWinningPhone?phone="+index,
                    dataType: "json",
                    success: function(data) {

                    },
                    error: function(errorMsg) {
                        alert("请求数据失败!");
                    }
                });
                i--;
            }
            if (target.hasClass("winner-valid")) {
                var index = li.find(".winner-mobile").html();
                $.ajax({
                    type: "POST",
                    url: "ConfirmWinningPhone?phone="+index,
                    dataType: "json",
                    success: function(data) {
                        //alert("中奖号码已被记录！");
                        var span = $("<span class='zj-record'>已确认！</span>");
                        li.find("button").remove().end().append(span);
                    },
                    error: function(errorMsg) {
                        alert("请求数据失败!");
                    }
                });
            }
        });
    } ());
})